package com.yyzw.activiti.modeler.controller;

import com.yyzw.activiti.modeler.model.DataGridRespVo;
import com.yyzw.activiti.modeler.model.ResponseVo;
import com.yyzw.activiti.modeler.model.SysRole;
import com.yyzw.activiti.modeler.model.SysRoleVo;
import com.yyzw.activiti.modeler.service.RoleService;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping(value = "/role")
public class RoleController {

    @Autowired
    RoleService roleService;

    @RequestMapping(value = "getSysRoleByPage")
    public DataGridRespVo getSysRoleByPage(HttpServletRequest request, String page, String rows){
        return roleService.getSysRoleByPage(Integer.parseInt(page),Integer.parseInt(rows));
    }

    @RequestMapping(value = "addRole")
    public ResponseVo addRole(@RequestBody  SysRoleVo sysRoleVo) throws  Exception{
        System.out.println(sysRoleVo.toString());
        SysRole sysRole = new SysRole();
        BeanUtils.copyProperties(sysRoleVo,sysRole);
        ResponseVo responseVo = roleService.addRole(sysRole,sysRoleVo.getMenuIds());
        return responseVo;
    }

    @RequestMapping(value = "updateRole")
    public ResponseVo editRole(@RequestBody  SysRoleVo sysRoleVo){
        SysRole sysRole = new SysRole();
        BeanUtils.copyProperties(sysRoleVo,sysRole);
        sysRole.setRoleId(Integer.parseInt(sysRoleVo.getRoleId()));
        return roleService.updateRole(sysRole,sysRoleVo.getMenuIds());
    }

    @RequestMapping(value = "delRoleById")
    public ResponseVo delRole(@Param("roleId") int roleId, HttpServletRequest request) throws  Exception{
       int delResult = roleService.delRole(roleId);
       if(delResult>0){
           return new ResponseVo(true,"删除成功");
       }
       return new ResponseVo(false,"删除失败");
    }

    @RequestMapping(value = "roleList")
    public ModelAndView rolePageShow(HttpServletRequest request,ModelAndView modelAndView) throws  Exception{
        modelAndView.setViewName("role/roleList");
        return modelAndView;
    }

    @RequestMapping(value = "showaLLRoleList")
    public List<SysRole> showaLLRoleList(HttpServletRequest request )throws  Exception{
        return roleService.getSYsRoleList();
    }

}
